
<!DOCTYPE html>
<html lang="">


<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
  <meta name="theme-color" content="#202020">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <script src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" async></script>
  
  
    <meta name="keywords" content="微信小程序,">
  

  
    <meta name="description" content="微信小程序打夯之旅(五)：分享 &amp; 分享到朋友圈">
  
  
  <link rel="icon" type="image/x-icon" href="/logo.png">
  <title>微信小程序打夯之旅(五)：分享 &amp; 分享到朋友圈 [ 复制粘贴工程师 ]</title>
  
    <!-- stylesheets list from config.yml -->
    
      <link rel="stylesheet" href="//cdn.bootcss.com/pure/1.0.0/pure-min.css">
    
      <link rel="stylesheet" href="/css/xoxo.css">
    
  
</head>


<body>
  <div class="nav-container">
    <nav class="home-menu pure-menu pure-menu-horizontal">
  <a class="pure-menu-heading" href="/">
    <img class="avatar" src="https://wx.qlogo.cn/mmopen/vi_32/AMpRacDgkTPogKk4gQ5IC3QezP55XoPpicQpnD9Vz3eNz6OPFYyFrFzCa4EGQGH65kPia5YJwQBBvvJpVwQ4lfVg/132">
    <span class="title">复制粘贴工程师</span>
  </a>

  <ul class="pure-menu-list clearfix">
      
          
            <li class="pure-menu-item"><a href="/" class="pure-menu-link">首页</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/search" class="pure-menu-link">搜索</a></li>
          
      
  </ul>

</nav>
  </div>

  <div class="container" id="content-outer">
    <div class="inner" id="content-inner">
      <div class="post-container">
  <article class="post" id="post">
    <header class="post-header text-center">
      <h1 class="title">
        微信小程序打夯之旅(五)：分享 &amp; 分享到朋友圈
      </h1>
      <span>
        
        <time class="time" datetime="2019-03-21T05:59:52.000Z">
        2019-03-21
      </time>
        
      </span>
      <span class="slash">/</span>
      <span class="post-meta">
      <span class="post-tags">
        <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/微信小程序/">微信小程序</a></li></ul>
      </span>
    </span>
      <span class="slash">/</span>
      <span class="read">
      <span id="busuanzi_value_page_pv"></span> 点击
    </span>
      <span class="slash">/</span>
    </header>

    <div class="post-content">
      <h4 id="页面基础分享能力"><a href="#页面基础分享能力" class="headerlink" title="页面基础分享能力"></a>页面基础分享能力</h4><p>基本的页面分享，只需要在页面中设置 <code>onShareAppMessage</code> 即可，如果分享的内容各个页面保持一致，建议进行一定的封装，如封装到 <code>Page</code> 的能力中。</p>
<a id="more"></a>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">App(&#123;</span><br><span class="line">  <span class="comment">// 获取分享卡片内容</span></span><br><span class="line">  getShareMessage(path) &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">      title: <span class="string">'分享标题'</span>,</span><br><span class="line">      imageUrl: <span class="string">'分享图片'</span>,</span><br><span class="line">      path: path || <span class="string">'分享默认路径'</span></span><br><span class="line">    &#125;;</span><br><span class="line">  &#125;,</span><br><span class="line">  enhancePage() &#123;</span><br><span class="line">    <span class="keyword">const</span> oPage = Page;</span><br><span class="line">    Page = <span class="function"><span class="params">config</span> =&gt;</span> oPage(<span class="built_in">Object</span>.assign(config, &#123;</span><br><span class="line">      getShareMessage: <span class="keyword">this</span>.getShareMessage,</span><br><span class="line">    &#125;));</span><br><span class="line">  &#125;,</span><br><span class="line">  onLaunch() &#123;</span><br><span class="line">    <span class="keyword">this</span>.enhancePage();</span><br><span class="line">  &#125;,</span><br><span class="line"></span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Page(&#123;</span><br><span class="line">  onShareAppMessage() &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">this</span>.getShareMessage(<span class="number">1</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h4 id="用户点击按钮主动触发分享"><a href="#用户点击按钮主动触发分享" class="headerlink" title="用户点击按钮主动触发分享"></a>用户点击按钮主动触发分享</h4><p>只需要设置按钮的 <code>open-type</code> 为 <code>share</code> 就可以自动触发页面的 <code>onShareAppMessage</code> 了。<br><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">open-type</span>=<span class="string">"share"</span>&gt;</span>分享按钮<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br></pre></td></tr></table></figure></p>
<h4 id="获取群聊中的分享信息"><a href="#获取群聊中的分享信息" class="headerlink" title="获取群聊中的分享信息"></a>获取群聊中的分享信息</h4><p>限于隐私保护，个人分享的信息已经不再能拿到了，但是我们可以获取分享所在群的信息。</p>
<ul>
<li>1.需要在分享页设置 <code>ticket</code> 信息</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">onLoad() &#123;</span><br><span class="line">  <span class="comment">// 配置分享，用以区分分享到个人还是群</span></span><br><span class="line">  wx.showShareMenu(&#123;<span class="attr">withShareTicket</span>: <span class="literal">true</span>&#125;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>2.在 <code>onShareAppMessage</code> 里获取群信息</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">onShareAppMessage() &#123;</span><br><span class="line">  <span class="keyword">return</span> &#123;</span><br><span class="line">    title: <span class="string">'分享'</span>,</span><br><span class="line">    path: <span class="string">'/pages/index'</span>,</span><br><span class="line">    success: <span class="function">(<span class="params">res</span>) =&gt;</span> &#123;</span><br><span class="line">      <span class="keyword">const</span> tickets = res.shareTickets;</span><br><span class="line">      <span class="keyword">if</span> (res.shareTickets) &#123;</span><br><span class="line">        wx.getShareInfo(&#123;</span><br><span class="line">          shareTicket: tickets[<span class="number">0</span>],</span><br><span class="line">          success: <span class="function">(<span class="params">data</span>) =&gt;</span> &#123;</span><br><span class="line">            <span class="built_in">console</span>.log(<span class="string">'分享到群: '</span>, data);</span><br><span class="line">          &#125;,</span><br><span class="line">        &#125;);</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">'分享到个人'</span>);</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>3.在 <code>APP.onLaunch</code> 或者 <code>App.onShow</code> 里获取群信息</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">App(&#123;</span><br><span class="line">  onShow: <span class="function">(<span class="params">options</span>) =&gt;</span> &#123;</span><br><span class="line">    wx.getShareInfo(&#123;</span><br><span class="line">      shareTicket: options.shareTicket,</span><br><span class="line">      success: <span class="function">(<span class="params">res</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="comment">// 请求微信服务器获取群信息</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h4 id="分享到朋友圈"><a href="#分享到朋友圈" class="headerlink" title="分享到朋友圈"></a>分享到朋友圈</h4><p>小程序是不支持直接分享到朋友圈的，只能通过保存图片并引导用户主动发送朋友圈。下面的例子是一个分享动态生成的图片。</p>
<ol>
<li>第一步：素材预下载，素材不支持远程URL，需要下载到本地（如小程序二维码和书封）</li>
</ol>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 获取小程序二维码</span></span><br><span class="line">downloadAppCode() &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function">(<span class="params">resolve, rej</span>) =&gt;</span> &#123;</span><br><span class="line">      wx.downloadFile(&#123;</span><br><span class="line">        url: <span class="string">'后台获取二维码接口'</span>,</span><br><span class="line">        success: <span class="function">(<span class="params">res</span>) =&gt;</span> &#123; resolve(res.tempFilePath); &#125;,</span><br><span class="line">        fail: <span class="function">(<span class="params">res</span>) =&gt;</span> &#123; resolve(<span class="string">''</span>); &#125;</span><br><span class="line">      &#125;);</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;,</span><br><span class="line"><span class="comment">// 下载文件</span></span><br><span class="line">downloadFile(url) &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function">(<span class="params">resolve, rej</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (url)&#123;</span><br><span class="line">      wx.downloadFile(&#123;</span><br><span class="line">        url: url,</span><br><span class="line">        success: <span class="function">(<span class="params">res</span>) =&gt;</span> &#123; resolve(res.tempFilePath); &#125;,</span><br><span class="line">      &#125;);</span><br><span class="line">    &#125; <span class="keyword">else</span> resolve();</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;,</span><br><span class="line"></span><br><span class="line"><span class="comment">// 下载所有书封到本地</span></span><br><span class="line">downloadCovers(books) &#123;</span><br><span class="line">  <span class="keyword">const</span> urls = books.map(<span class="function">(<span class="params">book</span>) =&gt;</span> <span class="keyword">this</span>.getBookCover(book.bookId));</span><br><span class="line">  <span class="built_in">Promise</span>.all([</span><br><span class="line">    <span class="keyword">this</span>.downloadFile(urls[<span class="number">0</span>]),</span><br><span class="line">    <span class="keyword">this</span>.downloadFile(urls[<span class="number">1</span>]),</span><br><span class="line">    <span class="keyword">this</span>.downloadFile(urls[<span class="number">2</span>]),</span><br><span class="line">    <span class="keyword">this</span>.downloadFile(urls[<span class="number">3</span>]),</span><br><span class="line">    <span class="keyword">this</span>.downloadFile(urls[<span class="number">4</span>]),</span><br><span class="line">    <span class="keyword">this</span>.downloadAppCode(),</span><br><span class="line">  ]).then(<span class="function">(<span class="params">res</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> appCode = res[res.length - <span class="number">1</span>];  <span class="comment">// 获取小程序二维码地址</span></span><br><span class="line">    res = res.splice(<span class="number">0</span>, res.length - <span class="number">1</span>);  <span class="comment">// 所有书封地址</span></span><br><span class="line">    res = res.filter(<span class="function"><span class="params">item</span> =&gt;</span> item);       <span class="comment">// 过滤空书封</span></span><br><span class="line">    <span class="keyword">this</span>.setData(&#123; <span class="attr">localCovers</span>: res, appCode &#125;);</span><br><span class="line">    <span class="keyword">this</span>.drawShareImg();</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ol start="2">
<li>第二步：通过canvas绘制需要被保存分享的图片</li>
</ol>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 绘制分享图片</span></span><br><span class="line">drawShareImg() &#123;</span><br><span class="line">    <span class="keyword">const</span> ctx = wx.createCanvasContext(<span class="string">'shareImg'</span>);</span><br><span class="line">    <span class="keyword">const</span> covers = <span class="keyword">this</span>.data.localCovers;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 背景</span></span><br><span class="line">    ctx.save()</span><br><span class="line">    ctx.setFillStyle(<span class="string">'white'</span>)</span><br><span class="line">    ctx.fillRect(<span class="number">0</span>, <span class="number">0</span>, <span class="number">260</span>, <span class="number">370</span>);</span><br><span class="line">    ctx.restore()</span><br><span class="line"></span><br><span class="line">    <span class="comment">// nickname</span></span><br><span class="line">    ctx.setFillStyle(<span class="string">'#111111'</span>);</span><br><span class="line">    ctx.setFontSize(<span class="number">14</span>);</span><br><span class="line">    ctx.setTextAlign(<span class="string">'center'</span>);</span><br><span class="line">    ctx.fillText(<span class="keyword">this</span>.data.userInfo.nickName, <span class="number">130</span>, <span class="number">42</span>, <span class="number">260</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 文案 心愿书屋</span></span><br><span class="line">    <span class="comment">// ctx.drawImage('../assets/images/share/share_icon_xysw.svg', 70, 52, 120, 30);</span></span><br><span class="line">    ctx.setFillStyle(<span class="string">'#111111'</span>);</span><br><span class="line">    ctx.setTextAlign(<span class="string">'center'</span>);</span><br><span class="line">    ctx.font = <span class="string">"30px SourceHanSerifCNMedium"</span>;</span><br><span class="line">    ctx.fillText(<span class="string">'心愿书屋'</span>, <span class="number">130</span>, <span class="number">82</span>, <span class="number">260</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 书封边框</span></span><br><span class="line">    ctx.setStrokeStyle(<span class="string">'rgba(0,0,0,0.1)'</span>);</span><br><span class="line">    <span class="keyword">if</span>(covers[<span class="number">3</span>]) ctx.strokeRect(<span class="number">21</span>, <span class="number">150</span>, <span class="number">42</span>, <span class="number">56</span>);</span><br><span class="line">    <span class="keyword">if</span>(covers[<span class="number">4</span>]) ctx.strokeRect(<span class="number">197</span>, <span class="number">150</span>, <span class="number">42</span>, <span class="number">56</span>);</span><br><span class="line">    <span class="keyword">if</span>(covers[<span class="number">1</span>]) ctx.strokeRect(<span class="number">51</span>, <span class="number">126</span>, <span class="number">60</span>, <span class="number">80</span>);</span><br><span class="line">    <span class="keyword">if</span>(covers[<span class="number">2</span>]) ctx.strokeRect(<span class="number">149</span>, <span class="number">126</span>, <span class="number">60</span>, <span class="number">80</span>);</span><br><span class="line">    <span class="keyword">if</span>(covers[<span class="number">0</span>]) ctx.strokeRect(<span class="number">91</span>, <span class="number">102</span>, <span class="number">78</span>, <span class="number">104</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 书封</span></span><br><span class="line">    <span class="keyword">if</span>(covers[<span class="number">3</span>]) ctx.drawImage(covers[<span class="number">3</span>], <span class="number">21</span>, <span class="number">150</span>, <span class="number">42</span>, <span class="number">56</span>);</span><br><span class="line">    <span class="keyword">if</span>(covers[<span class="number">4</span>]) ctx.drawImage(covers[<span class="number">4</span>], <span class="number">197</span>, <span class="number">150</span>, <span class="number">42</span>, <span class="number">56</span>);</span><br><span class="line">    <span class="keyword">if</span>(covers[<span class="number">1</span>]) ctx.drawImage(covers[<span class="number">1</span>], <span class="number">51</span>, <span class="number">126</span>, <span class="number">60</span>, <span class="number">80</span>);</span><br><span class="line">    <span class="keyword">if</span>(covers[<span class="number">2</span>]) ctx.drawImage(covers[<span class="number">2</span>], <span class="number">149</span>, <span class="number">126</span>, <span class="number">60</span>, <span class="number">80</span>);</span><br><span class="line">    <span class="keyword">if</span>(covers[<span class="number">0</span>]) ctx.drawImage(covers[<span class="number">0</span>], <span class="number">91</span>, <span class="number">102</span>, <span class="number">78</span>, <span class="number">104</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 矩形背景</span></span><br><span class="line">    ctx.rect(<span class="number">0</span>, <span class="number">226</span>, <span class="number">260</span>, <span class="number">66</span>);</span><br><span class="line">    ctx.setFillStyle(<span class="string">'#FFDCE7'</span>);</span><br><span class="line">    ctx.fill();</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 引号</span></span><br><span class="line">    ctx.drawImage(<span class="string">'/assets/images/share/share_icon_left.png'</span>, <span class="number">20</span>, <span class="number">242</span>, <span class="number">20</span>, <span class="number">12</span>);</span><br><span class="line">    ctx.drawImage(<span class="string">'/assets/images/share/share_icon_right.png'</span>, <span class="number">220</span>, <span class="number">264</span>, <span class="number">20</span>, <span class="number">12</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 二维码</span></span><br><span class="line">    ctx.drawImage(<span class="keyword">this</span>.data.appCode, <span class="number">108</span>, <span class="number">309</span>, <span class="number">44</span>, <span class="number">44</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 文案 我已经领了129书币</span></span><br><span class="line">    ctx.setFillStyle(<span class="string">'#773A4D'</span>);</span><br><span class="line">    ctx.font = <span class="string">"14px SourceHanSerifCNMedium"</span>;</span><br><span class="line">    ctx.setTextAlign(<span class="string">'center'</span>);</span><br><span class="line">    ctx.fillText(<span class="string">'我已经领了129书币'</span>, <span class="number">130</span>, <span class="number">254</span>, <span class="number">260</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 文案 天天领币，免费看书</span></span><br><span class="line">    ctx.setFillStyle(<span class="string">'#773A4D'</span>);</span><br><span class="line">    ctx.font = <span class="string">"14px SourceHanSerifCNMedium"</span>;</span><br><span class="line">    ctx.setTextAlign(<span class="string">'center'</span>);</span><br><span class="line">    ctx.fillText(<span class="string">'天天领币，免费看书'</span>, <span class="number">130</span>, <span class="number">274</span>, <span class="number">260</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 文案 识别小程序码</span></span><br><span class="line">    ctx.setFillStyle(<span class="string">'#9B9B9B'</span>);</span><br><span class="line">    ctx.setFontSize(<span class="number">12</span>);</span><br><span class="line">    ctx.setTextAlign(<span class="string">'left'</span>);</span><br><span class="line">    ctx.fillText(<span class="string">'识别小程序码'</span>, <span class="number">30</span>, <span class="number">337</span>, <span class="number">260</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 文案 进入心愿书屋</span></span><br><span class="line">    ctx.setFillStyle(<span class="string">'#9B9B9B'</span>);</span><br><span class="line">    ctx.font = <span class="string">"12px SourceHanSerifCNMedium"</span>;</span><br><span class="line">    ctx.setTextAlign(<span class="string">'left'</span>);</span><br><span class="line">    ctx.fillText(<span class="string">'进入心愿书屋'</span>, <span class="number">158</span>, <span class="number">337</span>, <span class="number">260</span>);</span><br><span class="line"></span><br><span class="line">    ctx.draw();</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>
<ol start="3">
<li>第三步：通过微信api下载图片到手机相册</li>
</ol>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">canvasToFile() &#123;</span><br><span class="line">    wx.canvasToTempFilePath(&#123;</span><br><span class="line">      x: <span class="number">0</span>,</span><br><span class="line">      y: <span class="number">0</span>,</span><br><span class="line">      width: <span class="number">260</span>,             <span class="comment">// 画布区域宽度</span></span><br><span class="line">      height: <span class="number">370</span>,            <span class="comment">// 画布区域高度</span></span><br><span class="line">      destWidth: <span class="number">260</span> * <span class="number">4</span>,     <span class="comment">// 保存图片宽度</span></span><br><span class="line">      destHeight: <span class="number">370</span> * <span class="number">4</span>,    <span class="comment">// 保存图片高度</span></span><br><span class="line">      canvasId: <span class="string">'shareImg'</span>,</span><br><span class="line">      success: <span class="function">(<span class="params">res</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">this</span>.saveImage(res.tempFilePath);</span><br><span class="line">      &#125;,</span><br><span class="line">      fail: <span class="function"><span class="keyword">function</span> (<span class="params">err</span>) </span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">'生成图片失败'</span>);</span><br><span class="line">      &#125;,</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;,</span><br><span class="line">  <span class="comment">// 保存图片</span></span><br><span class="line">saveImage(filePath) &#123;</span><br><span class="line">    wx.saveImageToPhotosAlbum(&#123;</span><br><span class="line">      filePath:filePath,</span><br><span class="line">      success: <span class="function">(<span class="params">res</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">this</span>.showSuccess(<span class="string">'图片保存成功'</span>);</span><br><span class="line">      &#125;,</span><br><span class="line">      fail: <span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">this</span>.showError(<span class="string">'图片保存到相册失败'</span>, <span class="string">'图片无法保存到相册，请稍后重试'</span>);</span><br><span class="line">      &#125;,</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>
<ul>
<li>本地图片处理注意事项 模拟器上都是骗人的</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">1. 不要使用相对路径，只能使用绝对路径</span><br><span class="line">2. 图片不支持svg不支持svg不支持svg</span><br><span class="line">3. 图片不支持base64，只支持本地图片和网络图片两种</span><br><span class="line">* 如果使用了相对路径、svg、base64，模拟器上运行顺畅的毫无破绽，但是在真机上是绘制不出来的！！！</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">还是老老实实转化成网络图片再下载吧！！！</span><br><span class="line">相关问题：https://developers.weixin.qq.com/community/develop/doc/00048c046f0028e62247f403651800?highLine=drawimage%2520base64</span><br></pre></td></tr></table></figure>
<ul>
<li>模拟器坑爹二连</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">明明在模拟器上特殊字体是有效的，为什么到了真机上就又变成了默认字体了？？？</span><br><span class="line">想要用字体？那就让设计切个图片给你吧</span><br></pre></td></tr></table></figure>
<h4 id="分享参数问题：新用户扫描后处理scene值"><a href="#分享参数问题：新用户扫描后处理scene值" class="headerlink" title="分享参数问题：新用户扫描后处理scene值"></a>分享参数问题：新用户扫描后处理scene值</h4><p>生成二维码时会带上scene值，当其他人扫描后可以从scene值中解析出有用的参数<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">decodeURLParams(str) &#123;</span><br><span class="line">  <span class="keyword">const</span> result = &#123;&#125;</span><br><span class="line">  <span class="keyword">const</span> params = str.split(<span class="string">'&amp;'</span>);</span><br><span class="line">  <span class="keyword">for</span> (<span class="keyword">let</span> param <span class="keyword">of</span> params) &#123;</span><br><span class="line">    <span class="keyword">const</span> keyValue = param.split(<span class="string">'='</span>);</span><br><span class="line">    result[keyValue[<span class="number">0</span>]] = keyValue[<span class="number">1</span>];</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> result;</span><br><span class="line">&#125;,</span><br><span class="line">onLoad() &#123;</span><br><span class="line">  <span class="keyword">let</span>  bookId = <span class="string">''</span>;</span><br><span class="line">  <span class="keyword">if</span> (options.bookId) bookId = options.bookId;</span><br><span class="line">  <span class="keyword">else</span> <span class="keyword">if</span> (options.scene) bookId = <span class="keyword">this</span>.decodeURLParams(<span class="built_in">decodeURIComponent</span>(options.scene)).bookId || <span class="string">''</span>;</span><br><span class="line">  <span class="keyword">this</span>.setData(&#123; <span class="attr">bookId</span>: bookId &#125;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>

    </div>

    <div>全文完。</div>
  </article>
  <div class="toc-container">
    
  <div id="toc" class="toc-article">
    <strong class="toc-title">目录</strong>
    <ol class="toc"><li class="toc-item toc-level-4"><a class="toc-link" href="#页面基础分享能力"><span class="toc-text">页面基础分享能力</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#用户点击按钮主动触发分享"><span class="toc-text">用户点击按钮主动触发分享</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#获取群聊中的分享信息"><span class="toc-text">获取群聊中的分享信息</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#分享到朋友圈"><span class="toc-text">分享到朋友圈</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#分享参数问题：新用户扫描后处理scene值"><span class="toc-text">分享参数问题：新用户扫描后处理scene值</span></a></li></ol>
  </div>


  </div>
</div>
<div class="copyright">
    <span>本作品采用</span>
    <a href="https://creativecommons.org/licenses/by/4.0/">知识共享署名 4.0 国际许可协议</a>
    <span>进行许可。 转载时请注明原文链接。</span>
</div>
<div class="share" style="width: 100%;">
  <!-- <img src="https://kevinofneu-blog-static.oss-cn-beijing.aliyuncs.com/static/2018-12-10-qrcode_for_gh_ffacf5722095_258.jpg" alt="Running Geek" style="margin: auto; display: block;"/>

  <div style="margin: auto; text-align: center; font-size: 0.8em; color: grey;">老铁们关注走一走，不迷路</div> -->

</div>

  
    <div class="post-nav">
      <div class="post-nav-item post-nav-next">
        
          <span>〈 </span>
          <a href="/2019/03/20/微信小程序增强Page能力/" rel="next" title="微信小程序打夯之旅(四)：Page能力增强">
          微信小程序打夯之旅(四)：Page能力增强
          </a>
        
      </div>
  
      <div class="post-nav-item post-nav-prev">
          
          <a href="/2019/03/28/微信小程序支付流程/" rel="prev" title="微信小程序打夯之旅(六)：支付流程">
            微信小程序打夯之旅(六)：支付流程
          </a>
          <span>〉</span>
        
      </div>
    </div>
  


    </div>

    
    <div id="vcomments" style="max-width: 800px; margin: 16px auto;"></div>
  </div>
  <footer class="footer text-center">
    <div id="bottom-inner">
        <a class="bottom-item" href="/">首页</a> |
        <a class="bottom-item" href="https://github.com/TJ-XiaJiaHao" target="_blank">GitHub</a> |
        <a class="bottom-item">联系邮箱：1452806@tongji.edu.cn</a>
    </div>
</footer>
  

<script>
  (function(window, document, undefined) {

    var timer = null;

    function returnTop() {
      cancelAnimationFrame(timer);
      timer = requestAnimationFrame(function fn() {
        var oTop = document.body.scrollTop || document.documentElement.scrollTop;
        if (oTop > 0) {
          document.body.scrollTop = document.documentElement.scrollTop = oTop - 50;
          timer = requestAnimationFrame(fn);
        } else {
          cancelAnimationFrame(timer);
        }
      });
    }

    var hearts = [];
    window.requestAnimationFrame = (function() {
      return window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function(callback) {
          setTimeout(callback, 1000 / 60);
        }
    })();
    init();

    function init() {
      css(".heart{z-index:9999;width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: absolute;}.heart:after{top: -5px;}.heart:before{left: -5px;}");
      attachEvent();
      gameloop();
      addMenuEvent();
    }

    function gameloop() {
      for (var i = 0; i < hearts.length; i++) {
        if (hearts[i].alpha <= 0) {
          document.body.removeChild(hearts[i].el);
          hearts.splice(i, 1);
          continue;
        }
        hearts[i].y--;
        hearts[i].scale += 0.004;
        hearts[i].alpha -= 0.013;
        hearts[i].el.style.cssText = "left:" + hearts[i].x + "px;top:" + hearts[i].y + "px;opacity:" + hearts[i].alpha + ";transform:scale(" + hearts[i].scale + "," + hearts[i].scale + ") rotate(45deg);background:" + hearts[i].color;
      }
      requestAnimationFrame(gameloop);
    }

    /**
     * 给logo设置点击事件
     * 
     * - 回到顶部
     * - 出现爱心
     */
    function attachEvent() {
      var old = typeof window.onclick === "function" && window.onclick;
      var logo = document.getElementById("logo");
      if (logo) {
        logo.onclick = function(event) {
          returnTop();
          old && old();
          createHeart(event);
        }
      }
      
    }

    function createHeart(event) {
      var d = document.createElement("div");
      d.className = "heart";
      hearts.push({
        el: d,
        x: event.clientX - 5,
        y: event.clientY - 5,
        scale: 1,
        alpha: 1,
        color: randomColor()
      });
      document.body.appendChild(d);
    }

    function css(css) {
      var style = document.createElement("style");
      style.type = "text/css";
      try {
        style.appendChild(document.createTextNode(css));
      } catch (ex) {
        style.styleSheet.cssText = css;
      }
      document.getElementsByTagName('head')[0].appendChild(style);
    }

    function randomColor() {
      // return "rgb(" + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + ")";
      return "#F44336";
    }

    function addMenuEvent() {
      var menu = document.getElementById('menu-main-post');
      if (menu) {
        var toc = document.getElementById('toc');
        if (toc) {
          menu.onclick = function() {
            if (toc) {
              if (toc.style.display == 'block') {
                toc.style.display = 'none';
              } else {
                toc.style.display = 'block';
              }
            }
          };
        } else {
          menu.style.display = 'none';
        }
      }
    }

  })(window, document);
</script>

  



</body>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src='//unpkg.com/valine/dist/Valine.min.js'></script>
<script type="text/javascript">
  new Valine({
    el: '#vcomments',
    appId: '8CwYqXS8IoFbvaA9PmgqGjTf-gzGzoHsz',
    appKey: 'z5bVoSPDPhd8hzlXvDey5Hpz',
    verify:false,
    placeholder: '评论留言'
  })
</script>
</html>
